[[...path]].page.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import React from 'react';
  2. import type { IUserHasId, IPagePopulatedToShowRevision } from '@growi/core';
  3. import type {
  4. GetServerSideProps, GetServerSidePropsContext,
  5. } from 'next';
  6. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  7. import Head from 'next/head';
  8. import superjson from 'superjson';
  9. import { useCurrentGrowiLayoutFluidClassName } from '~/client/services/layout';
  10. import { ShareLinkLayout } from '~/components/Layout/ShareLinkLayout';
  11. import GrowiContextualSubNavigationSubstance from '~/components/Navbar/GrowiContextualSubNavigation';
  12. import { DrawioViewerScript } from '~/components/Script/DrawioViewerScript';
  13. import { ShareLinkPageView } from '~/components/ShareLink/ShareLinkPageView';
  14. import { SupportedAction, SupportedActionType } from '~/interfaces/activity';
  15. import type { CrowiRequest } from '~/interfaces/crowi-request';
  16. import type { RendererConfig } from '~/interfaces/services/renderer';
  17. import type { IShareLinkHasId } from '~/interfaces/share-link';
  18. import type { PageDocument } from '~/server/models/page';
  19. import {
  20. useCurrentUser, useCurrentPageId, useRendererConfig, useIsSearchPage, useCurrentPathname, useIsNotFound,
  21. useShareLinkId, useIsSearchServiceConfigured, useIsSearchServiceReachable, useIsSearchScopeChildrenAsDefault, useDrawioUri, useIsContainerFluid,
  22. } from '~/stores/context';
  23. import loggerFactory from '~/utils/logger';
  24. import type { NextPageWithLayout } from '../_app.page';
  25. import {
  26. getServerSideCommonProps, generateCustomTitleForPage, getNextI18NextConfig, CommonProps,
  27. } from '../utils/commons';
  28. const logger = loggerFactory('growi:next-page:share');
  29. type Props = CommonProps & {
  30. shareLinkRelatedPage?: IShareLinkRelatedPage,
  31. shareLink?: IShareLinkHasId,
  32. isNotFound: boolean,
  33. isExpired: boolean,
  34. disableLinkSharing: boolean,
  35. isSearchServiceConfigured: boolean,
  36. isSearchServiceReachable: boolean,
  37. isSearchScopeChildrenAsDefault: boolean,
  38. drawioUri: string | null,
  39. rendererConfig: RendererConfig,
  40. };
  41. type IShareLinkRelatedPage = IPagePopulatedToShowRevision & PageDocument;
  42. superjson.registerCustom<IShareLinkRelatedPage, string>(
  43. {
  44. isApplicable: (v): v is IShareLinkRelatedPage => {
  45. return v != null
  46. && v.toObject != null
  47. && v.lastUpdateUser != null
  48. && v.creator != null
  49. && v.revision != null;
  50. },
  51. serialize: (v) => { return superjson.stringify(v.toObject()) },
  52. deserialize: (v) => { return superjson.parse(v) },
  53. },
  54. 'IShareLinkRelatedPageTransformer',
  55. );
  56. // GrowiContextualSubNavigation for shared page
  57. // get page info from props not to send request 'GET /page' from client
  58. type GrowiContextualSubNavigationForSharedPageProps = {
  59. page?: IPagePopulatedToShowRevision,
  60. isLinkSharingDisabled: boolean,
  61. }
  62. const GrowiContextualSubNavigationForSharedPage = (props: GrowiContextualSubNavigationForSharedPageProps): JSX.Element => {
  63. const { page, isLinkSharingDisabled } = props;
  64. return (
  65. <div data-testid="grw-contextual-sub-nav">
  66. <GrowiContextualSubNavigationSubstance currentPage={page} isLinkSharingDisabled={isLinkSharingDisabled}/>
  67. </div>
  68. );
  69. };
  70. const SharedPage: NextPageWithLayout<Props> = (props: Props) => {
  71. useCurrentPathname(props.shareLink?.relatedPage.path);
  72. useIsSearchPage(false);
  73. useIsNotFound(props.isNotFound);
  74. useShareLinkId(props.shareLink?._id);
  75. useCurrentPageId(props.shareLink?.relatedPage._id);
  76. useCurrentUser(props.currentUser);
  77. useRendererConfig(props.rendererConfig);
  78. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  79. useIsSearchServiceReachable(props.isSearchServiceReachable);
  80. useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
  81. useDrawioUri(props.drawioUri);
  82. useIsContainerFluid(props.isContainerFluid);
  83. const growiLayoutFluidClass = useCurrentGrowiLayoutFluidClassName(props.shareLinkRelatedPage);
  84. const pagePath = props.shareLinkRelatedPage?.path ?? '';
  85. const title = generateCustomTitleForPage(props, pagePath);
  86. return (
  87. <>
  88. <Head>
  89. <title>{title}</title>
  90. </Head>
  91. <div className={`dynamic-layout-root ${growiLayoutFluidClass} h-100 d-flex flex-column justify-content-between`}>
  92. <header className="py-0 position-relative">
  93. <GrowiContextualSubNavigationForSharedPage page={props.shareLinkRelatedPage} isLinkSharingDisabled={props.disableLinkSharing} />
  94. </header>
  95. <div id="grw-fav-sticky-trigger" className="sticky-top"></div>
  96. <ShareLinkPageView
  97. pagePath={pagePath}
  98. rendererConfig={props.rendererConfig}
  99. page={props.shareLinkRelatedPage}
  100. shareLink={props.shareLink}
  101. isExpired={props.isExpired}
  102. disableLinkSharing={props.disableLinkSharing}
  103. />
  104. </div>
  105. </>
  106. );
  107. };
  108. SharedPage.getLayout = function getLayout(page) {
  109. return (
  110. <>
  111. <DrawioViewerScript />
  112. <ShareLinkLayout>{page}</ShareLinkLayout>
  113. </>
  114. );
  115. };
  116. function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): void {
  117. const req: CrowiRequest = context.req as CrowiRequest;
  118. const { crowi } = req;
  119. const { configManager, searchService } = crowi;
  120. props.disableLinkSharing = configManager.getConfig('crowi', 'security:disableLinkSharing');
  121. props.isSearchServiceConfigured = searchService.isConfigured;
  122. props.isSearchServiceReachable = searchService.isReachable;
  123. props.isSearchScopeChildrenAsDefault = configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
  124. props.drawioUri = configManager.getConfig('crowi', 'app:drawioUri');
  125. props.rendererConfig = {
  126. isSharedPage: true,
  127. isEnabledLinebreaks: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
  128. isEnabledLinebreaksInComments: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
  129. adminPreferredIndentSize: configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
  130. isIndentSizeForced: configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),
  131. plantumlUri: process.env.PLANTUML_URI ?? null,
  132. blockdiagUri: process.env.BLOCKDIAG_URI ?? null,
  133. // XSS Options
  134. isEnabledXssPrevention: configManager.getConfig('markdown', 'markdown:rehypeSanitize:isEnabledPrevention'),
  135. xssOption: configManager.getConfig('markdown', 'markdown:rehypeSanitize:option'),
  136. attrWhiteList: JSON.parse(crowi.configManager.getConfig('markdown', 'markdown:rehypeSanitize:attributes')),
  137. tagWhiteList: crowi.configManager.getConfig('markdown', 'markdown:rehypeSanitize:tagNames'),
  138. highlightJsStyleBorder: configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  139. };
  140. }
  141. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  142. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  143. props._nextI18Next = nextI18NextConfig._nextI18Next;
  144. }
  145. function getAction(props: Props): SupportedActionType {
  146. let action: SupportedActionType;
  147. if (props.isExpired) {
  148. action = SupportedAction.ACTION_SHARE_LINK_EXPIRED_PAGE_VIEW;
  149. }
  150. else if (props.shareLink == null) {
  151. action = SupportedAction.ACTION_SHARE_LINK_NOT_FOUND;
  152. }
  153. else {
  154. action = SupportedAction.ACTION_SHARE_LINK_PAGE_VIEW;
  155. }
  156. return action;
  157. }
  158. async function addActivity(context: GetServerSidePropsContext, action: SupportedActionType): Promise<void> {
  159. const req: CrowiRequest = context.req as CrowiRequest;
  160. const parameters = {
  161. ip: req.ip,
  162. endpoint: req.originalUrl,
  163. action,
  164. user: req.user?._id,
  165. snapshot: {
  166. username: req.user?.username,
  167. },
  168. };
  169. await req.crowi.activityService.createActivity(parameters);
  170. }
  171. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  172. const req = context.req as CrowiRequest<IUserHasId & any>;
  173. const { crowi, params } = req;
  174. const result = await getServerSideCommonProps(context);
  175. if (!('props' in result)) {
  176. throw new Error('invalid getSSP result');
  177. }
  178. const props: Props = result.props as Props;
  179. try {
  180. const ShareLinkModel = crowi.model('ShareLink');
  181. const shareLink = await ShareLinkModel.findOne({ _id: params.linkId }).populate('relatedPage');
  182. if (shareLink == null) {
  183. props.isNotFound = true;
  184. }
  185. else {
  186. props.isNotFound = false;
  187. props.shareLinkRelatedPage = await shareLink.relatedPage.populateDataToShowRevision();
  188. props.isExpired = shareLink.isExpired();
  189. props.shareLink = shareLink.toObject();
  190. }
  191. }
  192. catch (err) {
  193. logger.error(err);
  194. }
  195. injectServerConfigurations(context, props);
  196. await injectNextI18NextConfigurations(context, props);
  197. await addActivity(context, getAction(props));
  198. return {
  199. props,
  200. };
  201. };
  202. export default SharedPage;